package com.budwk.app.sqfwDoc.codeGeneratorV2.handler;

import com.aspose.words.Document;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Paragraph;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.Data;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ZY.Wang
 * @since 2024/2/4
 */
public class PdfCheckBoxHandler {

    public static final String defaultSelectedSymbol = "☑";
    public static final String defaultNotSelectedSymbol = "□";

    public static Map<String, List<CheckBoxCondition>> getCheckBoxStyleFieldList(InputStream fileInputStream) {
        try {
            Document doc = new Document(fileInputStream);
            List<String> strings = readTextWithAspose(doc);
            return getCheckBoxFieldMap(strings);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Maps.newHashMap();
    }

    private static List<String> readTextWithAspose(Document document) {
        List<String> strList = Lists.newArrayList();
        NodeCollection<Paragraph> paragraphs = document.getChildNodes(NodeType.PARAGRAPH, true);
        for (Paragraph paragraph : paragraphs) {
            strList.add(paragraph.getText());
        }
        return strList;
    }

    private static Map<String, List<CheckBoxCondition>> getCheckBoxFieldMap(List<String> strList) {

        Map<String, List<CheckBoxCondition>> resultMap = Maps.newHashMap();

        Set<String> filter = Sets.newHashSet();

        // String regex = "(\\{\\{.+?==.+?(@.+?)?}})";
        String regex = "\\{\\{([^{}]+[=><][^{}]+(@[^{}]+)?)}}";
        Pattern pattern = Pattern.compile(regex);

        for (String content : strList) {
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                filter.add(matcher.group(0));
            }
        }

        for (String preResult : filter) {

            preResult = preResult.replace("{{", "").replace("}}", "");

            // 现在暂时只支持一种符号
            String segSymbol = "=";
            if (preResult.indexOf(">") > 0) segSymbol = ">";
            if (preResult.indexOf("<") > 0) segSymbol = "<";
            String[] strings = preResult.split(segSymbol);
            if (strings.length > 0) {
                String keyword = "";
                String conditionStr = "";
                String selectedSymbol = defaultSelectedSymbol;
                String notSelectedSymbol = defaultNotSelectedSymbol;

                keyword = strings[0];

                String conditionGroup = strings[1];

                // 制定了具体的符号
                if (conditionGroup.indexOf("@") > 0) {
                    String[] selectAbleCondition = conditionGroup.split("@");
                    if (selectAbleCondition.length > 0) {
                        conditionStr = selectAbleCondition[0];
                        try {
                            selectedSymbol = selectAbleCondition[1].substring(0, 1);
                            notSelectedSymbol = selectAbleCondition[1].substring(1, 2);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    conditionStr = conditionGroup;
                }

                CheckBoxCondition condition = new CheckBoxCondition();
                condition.setOrigExpr(preResult);
                condition.setKeyword(keyword);
                condition.setConditionStr(conditionStr);
                condition.setSelectedSymbol(selectedSymbol);
                condition.setNotSelectedSymbol(notSelectedSymbol);
                condition.setJudgeConditionSymbol(segSymbol);

                if (resultMap.containsKey(keyword)) {
                    resultMap.get(keyword).add(condition);
                } else {
                    resultMap.put(keyword, Lists.newArrayList(condition));
                }
            }
        }
        return resultMap;
    }


    @Data
    public static class CheckBoxCondition {
        private String origExpr;
        private String keyword;
        private String conditionStr;
        private String selectedSymbol;
        private String notSelectedSymbol;
        private String judgeConditionSymbol;
    }
}
